const _ = require('underscore');
const {BaseService} = require('../../basic.js')
const {
	CompanyModel
} = require('dx-func-entity')

module.exports = class Company extends BaseService {
	constructor(ctx) {
		super(ctx);
		this.entity = CompanyModel;		
	}
	// targetCompany() {
	//     //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
	// 	return this.hasMany('human-company', 'target_company', '_id').field('name,_id');
	// }
	childrens(){
		return this.hasMany('human-company','_id', 'parent_id' ).field('_id,name');
	}
	parent() {
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-company', 'parent_id', '_id').bindField({
			parentCompany: 'name',
		});
	}
	bestobj(){
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-talent', 'best_link', '_id').field('name,job,_id');
	}
	// products() {
	//     //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
	// 	return this.hasMany('human-picture', 'product', '_id').field('_id,name');
	// }
	// hrlists(){
	//     //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
	// 	return this.hasMany('human-talent', 'hrlist', '_id').field('name,_id');
	// }
	findByName(name){
		return this.where({name}).find();
	}
	findByNames(names=[]){
		return this.where({name:['in',names]}).field('name').limit(1000).select();
	}
	findByIds(_ids){
		return this.where('_id','IN',_ids).limit(1000).field('name,service,scale,headquarters,hrlist,create_date,user_id').select();
	}
	
	async findCompanyIds(param){
		const clist = await this.$dx.service.company.where('name','like',param.company_name).field('name').select();
		return clist;
	}
	async dealChildren({_id,children}){
		await this.where({parent_id:_id}).update({parent_id:''});
		if(_.isArray(children)&&!_.isEmpty(children)){
			return this.where({_id:['in',children]}).update({parent_id:_id});
		}
	}
	async loadTargetCompanyList(key,list){
		/*   查找  对标公司 */
		const tlist = [];
		_.each(list,item=>{
			if(_.isArray(item[key])){
				tlist.push(...item[key]);
			}
		})
		const _tids = _.filter(tlist,item=>_.isString(item));
		const companys = await this.findByIds(_tids);
		for (let i = 0; i < list.length; i++) {
			list[i][key+'s'] = (list[i][key]||[]).map(_id=>{
				if(_.isString(_id)){
					const company = _.findWhere(companys,{_id})
					return company
				}
				return _id;
			});
		}
		return list;
	}
	/*更新公司联动
	*/
	async updateCMerge({_id,_ids}){
		/*
		* 更新对标公司
		*/
		const result1 = await this.where({'target_company':['in',_ids]}).update({
			target_company:_id
		});
		/*
		 更新母公司
		*/
	    const result2 = await this.where({'parent_id':['in',_ids]}).update({
	    	parent_id:_id
	    });
		return result1&&result2;
	}
	/*更新 简历联动
	*/
	async updateTMerge({_id,_ids}){
		/*
		* 更新 HR
		*/
		const result = await this.where({'hrlist':['in',_ids]}).field('hrlist').select();
		if(result.length==0)return true;
		const list = _.map(result,company=>{
			const ids = _.without(company.hrlist,..._ids,null);
			return {
				_id:company._id,
				hrlist:[...ids,_id]
			}
		});
		
		for(let i = 0;i<list.length;i++){
		    await this.update(list[i]);
		}
		return true;
	}
	
	
}
